package com.sanji.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonValue;


/**
 * @Author qicai
 */
public class ResponseFactory {
    /**
     * 获得顺利返回的实例的工厂方法
     *
     * @author yangqc
     */
    public static <R> HttpResponse<R> getSuccussed(R result) {
        HttpResponse<R> response = new HttpResponse<R>();
        response.setResult(result);
        return response;
    }

    /**
     * 获得执行失败实例的工厂方法
     *
     * @Author yangqc
     **/
    public static <R> HttpResponse<R> getFailed(String errorMsg) {

        HttpResponse<R> response = new HttpResponse<R>();
        response.setStatus(HttpResponse.Status.ERROR);
        response.setErrorMsg(errorMsg);
        response.setSuccessMsg(null);
        return response;
    }

    public static <R> HttpResponse<R> getSuccussed(String sucMsg) {

        HttpResponse<R> response = new HttpResponse<R>();
        response.setStatus(HttpResponse.Status.SUCCESS);
        response.setSuccessMsg(sucMsg);
        return response;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class HttpResponse<R> {

        public enum Status {

            SUCCESS("success"), ERROR("error");

            private final String value;

            Status(String value) {
                this.value = value;
            }

            @JsonValue
            public String getValue() {
                return this.value;
            }
        }

        public HttpResponse() {
            this.result = null;
            this.successMsg = "SUCCESS";
            this.errorMsg = null;
        }

        public HttpResponse(R result) {
            this.result = result;
            this.successMsg = "SUCCESS";
            this.errorMsg = null;
        }

        // Default value is success
        private Status status = Status.SUCCESS;

        // Result data
        private R result;

        // Success message
        private String successMsg;

        // Error message
        private String errorMsg;

        private String errorCode;


        public Status getStatus() {
            return status;
        }

        public void setStatus(Status status) {
            this.status = status;
        }

        public R getResult() {
            return result;
        }

        public void setResult(R result) {
            this.result = result;
        }

        public String getSuccessMsg() {
            return successMsg;
        }

        public void setSuccessMsg(String successMsg) {
            this.successMsg = successMsg;
        }

        public String getErrorMsg() {
            return errorMsg;
        }

        public void setErrorMsg(String errorMsg) {
            this.status = Status.ERROR;
            this.errorMsg = errorMsg;
        }

        public String getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }
    }
}